package com.fanix5.javamvvm.widget;


import android.util.Log;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.Objects;

/**
 * 预加载工具
 */

//使用方法(注：刷新后需要重置方法):
//        mRecyclerViewHelper = new RecyclerViewHelper(mDatabind.recyclerView, 15);
//        mRecyclerViewHelper.setListener(() -> {
//        mViewModel.getData(false);
//        });



public class RecyclerViewHelper {

    private RecyclerView mRecycleView;

    private RecyclerViewHelperListener listener;

    private boolean mCanLoadMore = true;

    //  private boolean mIsOnLoading = false;

    private int mBottomCount = 1;
    int lastVisibleItem;
    int totalItemCount;

    public RecyclerViewHelper(RecyclerView recycleView) {
        this(recycleView, 10);
    }

    public RecyclerViewHelper(RecyclerView recycleView,
                               int bottomCount) {
        this.mRecycleView = recycleView;
        //  this.listener = listener;
        this.mBottomCount = bottomCount;

        //添加滑动监听

        mRecycleView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);

                 //  Log.e("TAG", "onScrolled: dx" + dx + "---dy" + dy);
                if (mRecycleView.getLayoutManager() instanceof LinearLayoutManager) {
                    lastVisibleItem =
                            ((LinearLayoutManager) Objects.requireNonNull(mRecycleView.getLayoutManager())).findLastVisibleItemPosition();
                    totalItemCount =
                            ((LinearLayoutManager) Objects.requireNonNull(mRecycleView.getLayoutManager())).getItemCount();
                } else if (mRecycleView.getLayoutManager() instanceof GridLayoutManager) {
                    lastVisibleItem =
                            ((GridLayoutManager) Objects.requireNonNull(mRecycleView.getLayoutManager())).findLastVisibleItemPosition();
                    totalItemCount =
                            ((GridLayoutManager) Objects.requireNonNull(mRecycleView.getLayoutManager())).getItemCount();
                }
                //总条目
                //lastVisibleItem >= totalItemCount - 20 表示剩下20个item实现预加载
                // dy>0 表示向下滑动,滑动距离
                if (lastVisibleItem >= totalItemCount - mBottomCount && dy > 0) {

                    if (mCanLoadMore) {
                        if (listener != null) {
                            listener.onLoadMore();
                        }
                        // getMoreData(mainRefreshLayout);
                        System.out.println("启动预加载！");
                        mCanLoadMore = false;
                    }

                }
            }
        });

    }

    /**
     * 是否可加载更多
     *
     * @param mCanLoadMore 是否可加载更多
     */
    public void setCanLoadMore(boolean mCanLoadMore) {
        this.mCanLoadMore = mCanLoadMore;
    }

    public void setListener(RecyclerViewHelperListener listener) {
        this.listener = listener;
    }

    public interface RecyclerViewHelperListener {
        void onLoadMore();
    }

}
